package com.xauto.pioneer.appuser.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.MainActivity;
import com.xauto.pioneer.appuser.activity.common.WebViewActivity;
import com.xauto.pioneer.appuser.model.BannerInfo;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;

/***
 * 广告页面
 * @author Lemon on Thu 11 Apr 12:45
 */
public class AdvertActivity extends BaseActivity {

    @Bind(R.id.ivAdvert) ImageView ivAdvert;
    @Bind(R.id.tvAdvertSkip) TextView tvAdvertSkip;
//    private ImageLoader imageLoader;
    private BannerInfo bannerInfo;

    /** 定时器*/
    private Timer mTimer;
    /** 定时器任务*/
    private TimerTask mTimerTask;

    @Override
    protected int getContentView() {
        return R.layout.ac_advert;
    }

    @Override
    protected void initView() {
        bannerInfo = (BannerInfo) getIntent().getSerializableExtra("banner_info");
        MyApplication.getDefaultFinalBitmap(mContext).display(ivAdvert, bannerInfo.image.source);
        startTimer();
    }

    @Override
    protected void initData() {
        ivAdvert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击图片跳转
                if(bannerInfo != null && bannerInfo.link_info != null) {
                    MyApplication.saveLinkInfo(bannerInfo.link_info);
                    stopTimer();
                    switch (bannerInfo.link_info.link_route){
                        case "outer_web":
                            startActivity(new Intent(mContext, MainActivity.class));
                            Uri uri = Uri.parse(bannerInfo.link_info.link_value);
                            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                            startActivity(intent);
                            finish();
                            break;
                        case "inner_web":
                            startActivity(new Intent(mContext, MainActivity.class));
                            WebViewActivity.startAction(mContext,true,bannerInfo.link_info.link_value);
                            finish();
                            break;
                        default:
                            skipMainAction();
                            break;
                    }
                }
            }
        });

        findViewById(R.id.tvAdvertSkip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳过
                stopTimer();
                skipMainAction();
            }
        });
    }
    /**
     * 跳转到主界面
     */
    private void  skipMainAction(){
        startActivity(new Intent(mContext, MainActivity.class));
        finish();
    }

    private void startTimer() {
        if (mTimer == null) {
            mTimer = new Timer();
        }
        if (mTimerTask == null) {
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    skipMainAction();
                }
            };
        }
        mTimer.schedule(mTimerTask, 3000);
    }

    public void stopTimer() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }

    @Override
    protected void onDestroy() {
        stopTimer();
        super.onDestroy();
    }
}
